package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerJDGoodInfoActivityComponent;
import com.wgts.wgts.dagger.module.JDGoodInfoActivityModule;
import com.wgts.wgts.localbean.LocalProductInfo;
import com.wgts.wgts.localbean.ProductImages;
import com.wgts.wgts.localbean.ProductInfoAttribute;
import com.wgts.wgts.localbean.ProductSpecifications;
import com.wgts.wgts.mvp.presenter.IJDGoodInfoActivityPersenter;
import com.wgts.wgts.mvp.view.IJDGoodinfoActivityView;
import com.wgts.wgts.mvp.view.activity.affrimorder.AffirmOrderActivity;
import com.wgts.wgts.mvp.view.activity.login.LoginActivity;
import com.wgts.wgts.netmodle.req.AffirmOrderProduct;
import com.wgts.wgts.netmodle.req.AffirmOrderRequest;
import com.wgts.wgts.netmodle.resp.ProductInfoEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.Tools;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JDGoodinfoActivity extends BaseActivity implements IJDGoodinfoActivityView, JDHeaderFragment.FragmentToActivity {


    float mAlpha1 = 1;
    float mAlpha2 = 0;
    int mPos;
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.title)
    LinearLayout title;
    @BindView(R.id.img_product)
    MZBannerView imgProduct;
    @BindView(R.id.tv_banner)
    TextView tvBanner;
    @BindView(R.id.relative_banner)
    RelativeLayout relativeBanner;
    @BindView(R.id.tv_util)
    TextView tvUtil;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.product_sale)
    TextView productSale;

    @BindView(R.id.tv_old_price)
    TextView tvOldPrice;
    @BindView(R.id.linear_old_price)
    LinearLayout linearOldPrice;
    @BindView(R.id.relative_product_price)
    RelativeLayout relativeProductPrice;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.linear_share)
    LinearLayout linearShare;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_product_attribute)
    TextView tvProductAttribute;
    @BindView(R.id.releative_select)
    RelativeLayout releativeSelect;
    @BindView(R.id.tv_trans)
    TextView tvTrans;
    @BindView(R.id.tv_product_trans)
    TextView tvProductTrans;
    @BindView(R.id.view_bg)
    View viewBg;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.save_view)
    LinearLayout saveView;
    @BindView(R.id.linear_ensur)
    LinearLayout linearEnsur;


    private ArrayList<Fragment> mFragments;


    private JDHeaderFragment iconFragment;


    private GoodsTypeLabsAdapter adapter;


    public static final String JOIN_TYPE = "JOIN_TYPE";

    //普通商品和限时抢购
    public static final String NOMAL = "NOMAL";

    public static final String PRODUCT_ID = "id";

    public static final String VIP_PRODUCT = "VIP_PRODUCT";

    public static final String VIP_SHARE = "VIP_SHARE";

    public static final String ACTION_PRODUCT = "ACTION_PRODUCT";

    public static final String PENDING = "PENDING";

    public static final String ACTIVE = "ACTIVE";

    public static final String DELETED = "DELETED";

    public static final String ACTION_STATE = "ACTION_STATE";

    public static final String ACTIVITYID = "activityId";

    public static final String BEGINTIME = "beginTime";

    public static final String ENDTIME = "endTime";

    public Integer activityId;

    public String beginTime;

    public String endTime;

    //全局定义
    private long lastClickTime = 0L;
    private static final int FAST_CLICK_DELAY_TIME = 500;  // 快速点击间隔


    @Inject
    IJDGoodInfoActivityPersenter persenter;


    private String id;

    private LinearLayoutManager linearLayoutManager;

    @Override
    public void init() {
        DaggerJDGoodInfoActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .jDGoodInfoActivityModule(new JDGoodInfoActivityModule(this))
                .build()
                .inject(this);


    }


    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.jd_product_info_activity);
        ButterKnife.bind(this);
        //EventBus.getDefault().register(this);

        linearEnsur.setEnabled(false);

        Intent intent = getIntent();
        id = intent.getStringExtra(PRODUCT_ID);
        persenter.nomalProductInfo(String.valueOf(id));
    }

    @Override
    public void productResult(ArrayList<ProductImages> banner, ArrayList<ProductImages> detailImages, LocalProductInfo productInfo, ArrayList<ProductInfoAttribute> attribute, ArrayList<ProductSpecifications> specifications) {
        linearEnsur.setEnabled(true);
        initBanner(banner);
        initProductInfo(productInfo);
        initRecycleView(detailImages, productInfo);
    }

    @Override
    public void selectResult(ProductInfoAttribute attribute) {

    }

    @Override
    public void mountPrice(String price) {

    }

    @Override
    public void mountOver() {

    }

    @Override
    public void mount(int mount) {

    }

    @Override
    public void collectionProductResult(int collected) {

    }

    @Override
    public void initCollectionResult(int collected) {

    }

    @Override
    public void addShopResult() {

    }

    @Override
    public void affirmOrder(AffirmOrderProduct affirmOrderProduct) {
        super.dialogDismiss();
        AffirmOrderRequest request = new AffirmOrderRequest();
        Intent intent = new Intent(JDGoodinfoActivity.this, AffirmOrderActivity.class);
        request.products.add(affirmOrderProduct);
        intent.putExtra(AffirmOrderActivity.Products, request);
        startActivity(intent);
    }

    @Override
    public void upDownProductResult(int state) {

    }

    @Override
    public void roduceError() {

    }

    @Override
    public void refreshProduct(ProductInfoEntity productInfoEntity) {

    }

    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void goToEventment() {

    }

    /*------------------------------------------通用方法------------------------------------------------*/


    private void initProductInfo(LocalProductInfo productInfo) {


        linearOldPrice.setVisibility(View.VISIBLE);

        tvPrice.setText(productInfo.retailPrice);
        tvOldPrice.setText("￥" + productInfo.marketPrice);
        tvOldPrice.setPaintFlags(tvOldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        //productSale.setText("已售：" + productInfo.sellCount);


//        String str1 = "微广自营";
//
//        SpannableString spannableString = new SpannableString("微广自营" + productInfo.productName);
//        int bgColor = Color.parseColor("#ff8041");
//        int textColor = Color.parseColor("#ff8041");
//        RoundBackgroundColorSpan span = new RoundBackgroundColorSpan(bgColor, textColor, 5);
//        spannableString.setSpan(span, 0, str1.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        tvProductAttribute.setText(productInfo.productStandard);

        tvProductName.setText(productInfo.productName);
    }

    private void initBanner(final ArrayList<ProductImages> banner) {
        DisplayMetrics outMetrics = Tools.metrics(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(outMetrics.widthPixels, outMetrics.widthPixels);
        relativeBanner.setLayoutParams(layoutParams);
        tvBanner.setText(1 + "/" + banner.size());
        imgProduct.setIndicatorVisible(false);
        imgProduct.start();
        imgProduct.setPages(banner, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new ProductBanner();
            }
        });

        imgProduct.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (banner != null && tvBanner != null) {
                    tvBanner.setText(i + 1 + "/" + banner.size());
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void initRecycleView(ArrayList<ProductImages> detailImages, LocalProductInfo productInfo) {
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(new GoodInfoPictureAdapter(this, detailImages, productInfo.productSubtitle));
    }


    @OnClick({R.id.back_btn_ly, R.id.linear_ensur, R.id.relativeLayout_kefu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.linear_ensur:
                if (AppInfor.getInstance().getRefreshToken() != null && !AppInfor.getInstance().getRefreshToken().equals("")) {
                    super.showDialog();
                    persenter.affirmOrder();
                } else {
                    Intent intent = new Intent(JDGoodinfoActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout_kefu:
                //Tools.call(JDGoodinfoActivity.this);
                break;
        }
    }
}
